<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Servicecore{
	
	function construyeBtn($propBtn){
	
		$btn="<a href='".$propBtn['url']."' class='".$propBtn['class']."' ".$propBtn['target'].">".$propBtn['name']."</a>&nbsp;";
	
		return $btn;
	}
	function generarUrlBtn($url,$indices){
		$cadena=$url;
		for($i=0;$i<count($indices);$i++){
			$cadena.='/'.$indices[$i];
				
		}
		return $cadena;
	}
	
	function arregloToString($propTabla){
	
	
		$arreglo=$propTabla['lista'];
		$indice=$propTabla['orden'];//parche para en vez de cadena con comas vaya defrente el arreglo
		$url=$propTabla['url'];
		$flagEdit=$propTabla['flagEdit'];
		$flagDelete=$propTabla['flagDelete'];
		$flagView=$propTabla['flagView'];
		$flagColorbox=$propTabla['flagColorbox'];
		$flagRestaurar=$propTabla['flagRestaurar'];
		$flagOther=$propTabla['flagOther'];
	
	
		//$indice=explode(',',$orden);
		$cadena='';
		$rows=NULL;
	
	
		for($i=0;$i<count($arreglo);$i++){
			for($j=0;$j<count($indice);$j++){
				if ($arreglo[$i][$indice[$j]] instanceof \DateTime) {
	
					$fecha=new Datetime();
					$fecha=$arreglo[$i][$indice[$j]];
					if($indice[$j]=='horasalida'){
						//echo intval($fecha->format('Y')).'<br>';
						if(intval($fecha->format('Y'))==-1)
							$cadena.='-,';
						else
							$cadena.='<!-- '.$fecha->format('hia').'-->'.$fecha->format('h:ia').',';
	
					}else
						$cadena.='<!-- '.$fecha->format('Ymd').'-->'.$fecha->format('d-m-Y').',';
				}else{
					if($arreglo[$i][$indice[$j]]=='') $arreglo[$i][$indice[$j]]=' - ';
					$cadena.=$arreglo[$i][$indice[$j]].',';
				}
			}
	
			$btnAction='';
			$z=0;
	
			if($flagView==1){
				$urlView=$this->generarUrl($url["view"],$url["viewIndices"]);
				$propBtn[$z]=array(	'url'=>$urlView,
						'class'=>'btn btn-metis-4',
						'name'=>$url["viewName"],
						'target'=>$url['viewTarget']);
				$z++;
			}
	
			if($flagEdit==1){
				$urlEdit=$this->generarUrl($url["edit"],$url["editIndices"]);
				$propBtn[$z]=array(	'url'=>$urlEdit,
						'class'=>'btn btn-metis-2',
						'name'=>$url["editName"],
						'target'=>$url['editTarget']);
				$z++;
			}
			if($flagDelete==1){
				$urlDelete=$this->generarUrl($url["delete"],$url["deleteIndices"]);
				$js='javascript:if(confirm("Esta seguro que desea eliminar este registro")){ location.href="'.$urlDelete.'/submit";}';
				$propBtn[$z]=array(	'url'=>$js,
						'class'=>'btn btn-metis-1',
						'name'=>$url["deleteName"],
						'target'=>$url['deleteTarget']);
				$z++;
			}
			if($flagRestaurar==1){
				$urlRestaurar=$this->generarUrl($url["restaurar"],$url["restaurarIndices"]);
				$js='javascript:if(confirm("Esta seguro que desea restaurar el registro seleccionado")){ location.href="'.$urlRestaurar.'/submit";}';
				$propBtn[$z]=array(	'url'=>$js,
						'class'=>'btn btn-metis-2',
						'name'=>$url['nameBtnRestaurar'],
						'target'=>$url['restaurarTarget']);
				$z++;
			}
			if($flagColorbox==1){
				for($kk=0;$kk<count($url['colorbox']);$kk++){
					$urlColorbox=$this->generarUrl($url["colorbox"][$kk]['url'],$url["colorbox"][$kk]["indices"]);
					$propBtn[$z]=array(	'url'=>$urlColorbox,
							'class'=>'btn btn-metis-2 iframe',
							'name'=>$url['colorbox'][$kk]['name'],
							'target'=>$url['colorbox'][$kk]['target']);
					$z++;
				}
			}
			if($flagOther==1){
				for($kk=0;$kk<count($url['other']);$kk++){
					$urlOther=$this->generarUrl($url["other"][$kk]['url'],$url["other"][$kk]["indices"]);
					$propBtn[$z]=array(	'url'=>$urlOther,
							'class'=>'btn btn-metis-5',
							'name'=>$url['other'][$kk]['name'],
							'target'=>$url['other'][$kk]['target']);
					$z++;
				}
			}
	
	
	
			$btnAction.=$this->construyeBtn($propBtn);
	
				
			$cadena=$btnAction;
			$rows[$i]=$cadena;
			$cadena='';
		}
	
		return $rows;
	
	}
	
	
	
	
	
	/*
	
	function verificaExisteIndice($indices){
		for($i=0;$i<count($indices);$i++){
			for($j=0;$j<count($this->indicesBase64);$j++)
				if($indices[$i]==$this->indicesBase64[$j]) return true;
		}
		return false;
	}
	
	function verificaIndice($indice){
		for($j=0;$j<count($this->indicesBase64);$j++){
			if($this->indicesBase64[$j]==$indice) return true;
		}
		return false;
	
	}
	
	function encripArrayResult($array){
		if(isset($array[0]))
			$indices=array_keys($array[0]);
		else
			$indices=array_keys($array);
		//var_dump($indices);
		if($this->verificaExisteIndice($indices)){
	
			for($i=0;$i<count($array);$i++){
				for($j=0;$j<count($indices);$j++){
					if($this->verificaIndice($indices[$j])){
							
						$array[$i][$indices[$j]]=base64_encode($array[$i][$indices[$j]]);
					}
				}
			}
		}
		return $array;
	
	}
	
	
	function addNumToArreglo($a){
			
		for($i=0;$i<count($a);$i++){
			$a[$i]['num']=($i+1);
		}
		return $a;
	}
	
	
	
	
	
	
	
	public static function TildesHtml($cadena)
	{
		return str_replace(array("á","é","í","ó","ú","ñ","Á","É","Í","Ó","Ú","Ñ"),
				array("&aacute;","&eacute;","&iacute;","&oacute;","&uacute;","&ntilde;",
						"&Aacute;","&Eacute;","&Iacute;","&Oacute;","&Uacute;","&Ntilde;"), $cadena);
	}
	
	
		
	function quitarNullSelect($arreglo){
		$j=0;
		$aux=NULL;
		for($i=0;$i<count($arreglo);$i++){
			$a=explode(',',$arreglo[$i]['value']);
			if($a[0]!=''){
				$aux[$j]=$arreglo[$i];
				$j++;
			}
		}
		return $aux;
	}
	*/
	
	
}
	
	
	
